package com.offcn.login;

import com.offcn.dao.StudentDao;
import com.offcn.entity.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;
@MultipartConfig
@WebServlet("/update2")
public class Update2 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StudentDao sd = new StudentDao();
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String tel = req.getParameter("tel");
        String bir = req.getParameter("bir");
        String sex = req.getParameter("sex");
        String spic = req.getParameter("spic");
        Part part = req.getPart("newspic");
        String path = "d:/pic";
        String filename = part.getSubmittedFileName();
        String pic = spic;
        if(!"".equals(filename)){
            filename= UUID.randomUUID()+filename;
            pic = filename;
            part.write(path+File.separator+pic);
            File f = new File(path+File.separator+spic);
            f.delete();
        }

        Student s2 = new Student(name,Integer.parseInt(age),tel,DateUtil.dateformat(bir),Integer.parseInt(sex),pic);
        boolean b = sd.updateStu(s2,Integer.parseInt(id));
        if(b){
            resp.sendRedirect("student");
        }
    }
}
